package com.example.maxboxtwo.components.activities;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.maxboxtwo.R;
import com.example.maxboxtwo.components.function.Gradienter.GradienterView;
import com.example.maxboxtwo.utils.listeners.HorizontalListener;

public class Gradienter extends AppCompatActivity {
    private HorizontalListener horizontalListener;
    private GradienterView gradienterView;
    private SensorManager manager;
    private Sensor accelerate;
    private Sensor geomagetic;
    private TextView leftText,rightText;
    private ImageView lock;
    private boolean flag = true;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gradienter_activity);
        gradienterView = findViewById(R.id.gradient);
        leftText = findViewById(R.id.left);
        rightText = findViewById(R.id.right);
        lock = findViewById(R.id.lock);

        horizontalListener = new HorizontalListener(leftText,rightText,gradienterView);
        manager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        accelerate = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        geomagetic = manager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

        //create a listener for button of lock
        lock.setOnClickListener((v)->{
                if(flag){
             lock.setImageResource(R.drawable.tools_level_lock_on);
             flag = false;
             //unregister the listener
             onPause();
                }else{
            flag = true;
            lock.setImageResource(R.drawable.tools_level_lock_off);
            //register the listener
            onResume();
                }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("result",manager+"");
        manager.registerListener(horizontalListener,accelerate, Sensor.TYPE_ACCELEROMETER);
        manager.registerListener(horizontalListener,geomagetic,Sensor.TYPE_MAGNETIC_FIELD);
    }


    @Override
    protected void onPause() {
        super.onPause();
        manager.unregisterListener(horizontalListener);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
